package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.Distributionmode;
import cn.b.sky.mes.mm.model.PFEP;
import cn.b.sky.mes.mm.model.StorageLocation;
import cn.b.sky.mes.mm.model.Warehouse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.PullStrategy;
import cn.b.sky.mes.mm.model.condition.PFEPCondition;
import cn.b.sky.mes.mm.service.DistributionmodeService;
import cn.b.sky.mes.mm.service.PFEPService;
import cn.b.sky.mes.mm.service.PullStrategyService;
import cn.b.sky.mes.mm.service.StorageLocationService;
import cn.b.sky.mes.mm.service.WarehouseService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/pfep/*")
public class PFEPController extends SecurityController {

  @Resource
  PFEPService pFEPService;

  @Resource
  DistributionmodeService distributionmodeService;

  @Resource
  PullStrategyService pullStrategyService;
  @Resource
  WarehouseService warehouseService;
  @Resource
  StorageLocationService storageLocationService;
  
  @RequestMapping("pfepList")
  public String pfepList(@ModelAttribute("condition") PFEPCondition condition,
      String navTabId, Model model) {
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    condition.setSiteCode(this.getSiteCode());
    List<Distributionmode> distributionModes=distributionmodeService.selectAlldistributionmode(true);
    List<PullStrategy> pullStrategys=pullStrategyService.selectAllPullStrategy(true);
    List<PFEP> pFEPs = pFEPService.selectPFEPByCondition(condition);
    int count = pFEPService.selectPFEPCount(condition);
    condition.setData(pFEPs);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute("distributionModes", distributionModes);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("pullStrategys", pullStrategys);
    return "mm/pfep/pfep_list";
  }
  
  @RequestMapping("getDistributionQuantityByMaterialCode")
  @ResponseBody
  public DWZResult getDistributionQuantityByMaterialCode(String materialCode) {
    if (StringUtils.isBlank(materialCode)) {
      return ajaxDone(0);
    }else {
      PFEP pfep = pFEPService.selectPFEP(getSiteCode(), materialCode);
      if(pfep==null){
        return ajaxDone(0);
      }
      return ajaxDone(pfep.getDistributionQuantity());
    }   
  }
  @RequestMapping("addPfep")
  public String addPfep(String navTabId, Model model) {
    model.addAttribute("navTabId", navTabId);
    List<Distributionmode> distributionModes=distributionmodeService.selectAlldistributionmode(false);
    List<PullStrategy> pullStrategys=pullStrategyService.selectAllPullStrategy(false);
    model.addAttribute("distributionModes", distributionModes);
    model.addAttribute("pullStrategys", pullStrategys);
    List<Warehouse> warehouses = warehouseService.selectUnLockWarehouseListBySiteCode(getSiteCode());
    model.addAttribute("warehouses", warehouses);
    return "mm/pfep/pfep_add";
  }
  
  @RequestMapping(value = "warehouseChange")
  @ResponseBody
  public DWZResult warehouseChange(Model model, String navTabId, String warehousCode) {
    Warehouse warehouse = warehouseService.getWarehouse(warehousCode, getSiteCode());
    List<StorageLocation> locations = null;
    if (warehouse!=null&&warehouse.getLocationManageFlag().intValue() == 1) {
      locations = storageLocationService.selectStorageLocationByWarehouse(getSiteCode(), warehousCode);
    }
    return ajaxDone(locations);
  }
  
  @RequestMapping("isExistsPfep")
  @ResponseBody
  public Boolean isExistsPfep(Model model, String materialCode) {
    if (StringUtils.isBlank(materialCode)) {
      return false;
    }
    PFEP pFEP = pFEPService.selectPFEP(getSiteCode(), materialCode);
    if (pFEP != null) {
      return false;
    }
    return true;
  }
  
  @RequestMapping("savePfep")
  @ResponseBody
  public DWZResult savePfep(PFEP pFEP, Model model, String id, String navTabId) {
    if (pFEP == null || StringUtils.isBlank(pFEP.getMaterialCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "parameterNull.M"));
    }
    pFEP.setSiteCode(this.getSiteCode());
    this.setCreateInfo(pFEP);
    if (StringUtils.isBlank(id)) {
      pFEPService.insertPFEP(pFEP);
    } else {
      pFEPService.updatePFEP(pFEP);
    }
    return dialogAjaxDone(navTabId, null, null, null);
  }
  
  @RequestMapping("editPfep")
  public String editDistributionTriggerMode(String navTabId, Model model, String id) {
    model.addAttribute("navTabId", navTabId);
    PFEP pFEP = pFEPService.selectPFEP(getSiteCode(), id);
    model.addAttribute("pfep", pFEP);
    List<Distributionmode> distributionModes=distributionmodeService.selectAlldistributionmode(false);
    List<PullStrategy> pullStrategys=pullStrategyService.selectAllPullStrategy(false);
    model.addAttribute("distributionModes", distributionModes);
    model.addAttribute("pullStrategys", pullStrategys);
    List<Warehouse> warehouses = warehouseService.selectUnLockWarehouseListBySiteCode(getSiteCode());
    model.addAttribute("warehouses", warehouses);
    return "mm/pfep/pfep_add";
  }
  
  @RequestMapping("viewPfep")
  public String viewPfep(String navTabId, Model model, String id) {
    model.addAttribute("navTabId", navTabId);
    PFEP pFEP = pFEPService.selectPFEP(getSiteCode(), id);
    model.addAttribute("pfep", pFEP);
    List<Distributionmode> distributionModes=distributionmodeService.selectAlldistributionmode(false);
    List<PullStrategy> pullStrategys=pullStrategyService.selectAllPullStrategy(false);
    model.addAttribute("distributionModes", distributionModes);
    model.addAttribute("pullStrategys", pullStrategys);
    List<Warehouse> warehouses = warehouseService.selectUnLockWarehouseListBySiteCode(getSiteCode());
    model.addAttribute("warehouses", warehouses);
    return "mm/pfep/pfep_view";
  }
  
  @RequestMapping("deletePfep")
  @ResponseBody
  public DWZResult deletePfep(String navTabId, Model model, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = pFEPService.deletePFEP(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }
}
